package com.taikang.common.util.sercurity;

import java.security.MessageDigest;
import java.security.Security;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MD5Encrypter {
    
    private static String DEFAULT_JCE = "com.sun.crypto.provider.SunJCE";
    
    private static String IBM_JCE = "com.ibm.crypto.provider.IBMJCE";
    
    protected static final Log log = LogFactory.getLog(MD5Encrypter.class);
    
    static {
        
        try {
            Security.addProvider((Provider) Class.forName(DEFAULT_JCE)
            .newInstance());
        } catch (Exception e) {
            log.info(e);
            try {
                Security.addProvider((Provider) Class.forName(IBM_JCE)
                .newInstance());
            } catch (Exception ex) {
                log.info(ex);
            }
        }
    }
    
    private static String hexDigit(byte x) {
        StringBuffer sb = new StringBuffer();
        char c;
        // First nibble
        c = (char) ((x >> 4) & 0xf);
        if (c > 9) {
            c = (char) ((c - 10) + 'a');
        } else {
            c = (char) (c + '0');
        }
        sb.append(c);
        // Second nibble
        c = (char) (x & 0xf);
        if (c > 9) {
            c = (char) ((c - 10) + 'a');
        } else {
            c = (char) (c + '0');
        }
        sb.append(c);
        return sb.toString();
    }
    
    public static String encrypt(String content) {
        try {
            MessageDigest algorithm = null;
            algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            if (content != null) {
                algorithm.reset();
                algorithm.update(content.getBytes());
                byte digest[] = algorithm.digest();
                StringBuffer hexString = new StringBuffer();
                int digestLength = digest.length;
                for (int i = 0; i < digestLength; i++) {
                    hexString.append(hexDigit(digest[i]));
                }
                return hexString.toString();
            } else {
                return "";
            }
        } catch (NoSuchAlgorithmException ex) {
            return content;
        }
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        
        System.out.println("MD5 Test suite:");
        System.out.println("MD5(\"\"):" + MD5Encrypter.encrypt(""));
        System.out.println("MD5(\"a\"):" + MD5Encrypter.encrypt("a"));
        System.out.println("MD5(\"abc\"):" + MD5Encrypter.encrypt("abc"));
        System.out.println("MD5(\"123\"):" + MD5Encrypter.encrypt("123"));
        System.out.println("MD5(\"password\"):" + MD5Encrypter.encrypt("password"));
        System.out.println("MD5(\"message digest\"):"
            + MD5Encrypter.encrypt("message digest"));
        System.out.println("MD5(\"abcdefghijklmnopqrstuvwxyz\"):"
            + MD5Encrypter.encrypt("abcdefghijklmnopqrstuvwxyz"));
        System.out
            .println("MD5(\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\"):"
            + MD5Encrypter
            .encrypt("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"));
        
        // long start = System.currentTimeMillis();
        // for (int i = 0; i < 10000; i++) {
        // System.out.println("MD5 Encrpt " + i + " is :"
        // + MD5Encrypter.encrypt("" + i));
        // }
        // long end = System.currentTimeMillis();
        // System.err.println("Using time is===" + (end - start) + "ms");
    }
}
